public class MySingleList {
    static class ListNode {
        public int val;//节点的值域
        public ListNode next;//下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }

    }
    public ListNode head;//表示当前链表的头节点


    public void createList() {

        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;//头节点

    }




    //头插法
    public void addFirst(int data){
        //再插入的时候，先绑定后面的节点信息
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }


    //尾插法
    public void addLast(int data){
        ListNode cur = head;
        ListNode node = new ListNode(data);
        //当列表中一个元素都没有时
        if (cur == null){
            head = node;
            return;
        }

        while (cur.next !=null){
            cur=cur.next;
        }
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        if(index < 0 || index > size()){
            System.out.println("index 不合法");
            return;
        }
        if(index == 0){
            addFirst(data);
        }
        if(index == size()){
            addLast(data);
        }
        ListNode cur = head;
        //找到要插入的前一个节点
       while(index -1 != 0){
           cur.next = cur;
           index--;
       }
       node.next = cur.next;
       cur.next = node;

    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur!=null){
            cur=cur.next;
            if(cur.val == key ){
                return true;
            }
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if(cur == null) {
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    /**
     * 找到关键字 key的前驱
     * @param key
     * @return
     */
    private ListNode searchPrev(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode pre = head;
        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
                cur = cur.next;
            }else{
                pre = cur;
                cur = cur.next;
            }
        }
        //单独判断头节点
        if(head.val == key){
            //删除头节点
            head = head.next;
        }


    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }


    //打印链表
    public void display(){
        ListNode cur = head;
       while (cur!=null){
           System.out.print(cur.val+" ");
           cur=cur.next;
       }
        System.out.println("");
    }


    //清除链表
    public void clear(){
        //直接将head置空
        this.head = null;

    }

    //链表的中间值
    public ListNode middleNode() {
        ListNode fast =head;
        ListNode slow =head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //反转链表
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        if(head.next == null) return head;
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            //记录需要翻转的节点的下一个节点
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }



    //输入一个链表，输出该链表中倒数第k个结点。
    public ListNode countBackwards(int k) {
        if (k <= 0 || head == null) {
            System.out.println("输入的k不合法");
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        while(k-1>0){
            fast = fast.next;
            k--;
            if(fast == null ){
                return null;
            }
        }
        while(fast.next != null){
            fast =fast.next;
            slow = slow.next;
        }
        return slow;
    }
//        ListNode cur = head;
//
//        while (size() - k > 0) {
//            cur = cur.next;
//            k++;
//        }
//        return cur;
//    }


    //编写代码，以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前
    public ListNode partition(ListNode pHead, int x) {
        // write code here

        ListNode bs = null;
        ListNode be = null;

        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;

        while (cur != null) {
            if (cur.val < x) {
                //第一次插入数据
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null){
            return as;
        }
        //防止最大的数据不在最后一个
        if(ae != null){
            ae.next = null;
        }
        be.next = as;
        return bs;

    }



    // 链表的回文结构。
    public boolean chkPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow =head;
        //找到中间节点slow
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        while(cur != null){
            //记录下要反转的下一个节点
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            //偶数情况下
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }


    // 输入两个链表，找出它们的第一个公共结点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        int lenA = 0;
        int lenB = 0;

        ListNode pl = headA;
        ListNode ps = headB;

        while(pl != null){
            lenA++;
            pl = pl.next;
        }

        while(ps != null){
            lenB++;
            ps = ps.next;
        }

        pl = headA;
        ps = headB;

        int len = lenA -lenB;
        if(len < 0){
            pl = headB;
            ps = headA;
            len =lenB - lenA;
        }

        //2.让长的先走len步
        for(int i =0; i<len; i++ ){
            pl = pl.next;
        }
        //找到相遇的点
        while(pl != ps){
            pl = pl.next;
            ps = ps.next;
        }
        return pl;
    }

    //创建一个环
    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;

            
        }
        //cur.next == null;
        cur.next = head.next;
    }

    //给定一个链表，判断链表中是否有环。
    public boolean hasCycle(ListNode head) {
        if(head == null) return false;
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null ){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }


    //判断环的入口点
    public ListNode detectCycle(ListNode head) {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null ){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
               break;
            }
        }
       if(fast == null || fast.next == null) {
           return null;
       }
       fast = head;
       while(fast != slow){
           fast = fast.next;
           slow = slow.next;
       }
       return fast;
    }



    //删除链表中重复的节点
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode cur = pHead;
        ListNode nHead = new ListNode(1);
        ListNode tmpHead = nHead;
        while(cur != null){
            if(cur.next != null && cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                cur = cur.next;
            }else{
                tmpHead.next = cur;
                tmpHead = tmpHead.next;
                cur = cur.next;
            }
        }
        //手动把最后一个节点的next置为空
        tmpHead.next = null;
        return nHead.next;
    }
}
